package com.client.act;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.client.bean.Call;
import com.client.bean.CallMessage;
import com.client.common.SpinnerProgressDialog;
import com.client.task.CallMessagesTask;
import com.clinet.cache.AsyncImageLoader;
import com.clinet.cache.Log;

public class TypeAct extends BaseAct {
	private Context mContext = TypeAct.this;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.typeact);
		init();
		onclick();
		getData();
	}

	public static AsyncImageLoader mAsyncImageLoader = null;

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mAsyncImageLoader.clearImageMap();
		mAsyncImageLoader.shutdownThreadPool();
	}

	private void getData() {
//		SpinnerProgressDialog.showDialog(this,"正在获取数据,请稍后...");
		CallMessagesTask.Callback callback = new CallMessagesTask.Callback() {
			@Override
			public void onComplete(final Integer result) {

				SpinnerProgressDialog.dismissDialog();
				switch (result) {
				case 1:
					initListView();

					break;
				case 0:

					break;
				}
			}
		};
		mCallMessagesTask = new CallMessagesTask(mContext, callback);
		mCallMessagesTask.execute(mCall);
	}

	private CallMessagesTask mCallMessagesTask = null;

	private void onclick() {

	}

	private Call mCall = new Call();

	private ListView listview = null;
	private LayoutInflater layoutFlater = null;

	private void init() {
		layoutFlater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mAsyncImageLoader = new AsyncImageLoader();
		listview = (ListView) findViewById(R.id.listview);
	}// -----------------------------列表----------------------S

	public void initListView() {
		if (mCall == null || mCall.getTotalNum() <= 0) {
			return;
		}
		listview = (ListView) findViewById(R.id.listview);
		listview.setDivider(null);
		mListViewAdapter = new TimeListAdapter();
		listview.setAdapter(mListViewAdapter);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {

			}
		});

	}

	private TimeListAdapter mListViewAdapter = null;

	public class TimeListAdapter extends BaseAdapter {

		public TimeListAdapter() {
			super();

		}

		public int getCount() {
			return mCall.getTotalNum();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertview, ViewGroup parent) {
			final ViewHolderListViewItem holder;
			if (convertview == null) {
				convertview = layoutFlater.inflate(R.layout.call_list_item,
						null);
				holder = new ViewHolderListViewItem();
				holder.birdName = (TextView) convertview
						.findViewById(R.id.birdName);
				holder.edit = (ImageView) convertview.findViewById(R.id.edit);
				holder.line = (ImageView) convertview.findViewById(R.id.line);
				convertview.setTag(holder);
			} else {
				holder = (ViewHolderListViewItem) convertview.getTag();
			}

			final CallMessage bean = mCall.getItems().get(position);
			holder.birdName.setText(bean.name);
			holder.edit.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent();
					intent.setAction("android.intent.action.CALL");
					intent.setData(Uri.parse("tel:" + bean.num));// mobile为你要拨打的电话号码，模拟器中为模拟器编号也可
					Log.i("--gophone----------" + bean.num);
					startActivity(intent);

				}
			});
			// 载入图片
			return convertview;
		}
	}

	private static class ViewHolderListViewItem {
		public TextView birdName = null;
		public ImageView edit = null;
		public ImageView line = null;
	}

	// -----------------------------网格----------------------E
}